package com.zhouchao.messenger2;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = ">>>>MainActivity";
    private EditText editText;
    private TextView textView;
    private boolean isBind=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText=findViewById(R.id.editText);
        textView=findViewById(R.id.text1);
    }

    //用来接受服务器返回的结果
    private Handler acceptHandler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            Log.i(TAG, "handleMessage: 接收到了服务器计算后的数据");
            Log.i(TAG, "handleMessage: what="+message.what);
            Log.i(TAG, "handleMessage: arg1="+message.arg1);
            textView.setText("结果是:"+message.arg1);
            return true;
        }
    });
    //把此对象联通消息发送给服务器,服务器拿到后用来发送结果
    private Messenger acceptMessenger=new Messenger(acceptHandler);
    //用服务器返回的IBind构造,用来给服务器发送消息
    private Messenger sendMessenger;

    private ServiceConnection conn=new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.i(TAG, "onServiceConnected: 连接成功");
            sendMessenger=new Messenger(iBinder);
            isBind=true;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.i(TAG, "onServiceDisconnected: 连接意外关闭");
            isBind=false;
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        if(!isBind){
            Log.i(TAG, "onStart: ");
            Intent service=new Intent();
            service.setAction("com.zg.action.ComputeService");
            service.setPackage("com.zhouchao.messenger1");
            bindService(service,conn, Context.BIND_AUTO_CREATE);

        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: ");
        if(isBind){
            unbindService(conn);
            isBind=false;
        }
    }

    public void sendForResult(View view) {
        if(!isBind || sendMessenger==null){
            Toast.makeText(this, "正在尝试连接请稍后再试", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            Integer input=Integer.parseInt( editText.getText().toString().trim());
            Message msg=Message.obtain();
            msg.what=100;
            msg.arg1=input;
            msg.replyTo=acceptMessenger;
            try {
                sendMessenger.send(msg);
            } catch (RemoteException e) {
                e.printStackTrace();
                Toast.makeText(this, "发送失败", Toast.LENGTH_SHORT).show();
            }


        } catch (NumberFormatException e) {
            e.printStackTrace();
            Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
        }

    }
}
